---@type widget_widget
local Widget = require 'widgets/widget'
---@type widget_image
local Image = require 'widgets/image'
-- ---@type widget_button
-- local Button = require "widgets/button"
---@type widget_imagebutton
local ImageButton = require 'widgets/imagebutton'
---@type widget_text
local Text = require('core_webber_the_trainer/widgets/webbert_txt')
---@type widget_textedit
local TextEdit = require "widgets/textedit"
-- ---@type widget_uianim
-- local UIAnim = require "widgets/uianim"
---@type widget_truescrollarea
local TrueScrollArea = require "widgets/truescrollarea"

local TEMPLATES = require "widgets/redux/templates"

---@type widget_scrollablelist
local webbert_scrollablelist = require('core_webber_the_trainer/widgets/webbert_scrollablelist')


local data_webbert_gameconsole = require('core_webber_the_trainer/data/webbert_gameconsole')


-- 左侧菜单 单条目
---@class widget_webbert_gameconsole_item : widget_widget
---@overload fun(info:string,xml:string,tex:string,fn:function):widget_webbert_gameconsole_item
---@field btn widget_imagebutton
---@field logo widget_image
---@field label widget_text
local webbert_gameconsole_item = Class(Widget,
---@param self widget_webbert_gameconsole_item
function(self,info,xml,tex,fn)
    Widget._ctor(self, 'webbert_gameconsole_item')

    self.btn = self:AddChild(ImageButton('images/global_redux.xml','spinner_background_normal.tex','spinner_background_edited.tex','spinner_background_normal.tex'))
    self.btn:SetScale(.8,1)
    self.logo = self:AddChild(Image(xml,tex,tex))
    self.logo:SetScale(.35,.35)
    self.logo:SetPosition(-100,0)
    self.logo:SetClickable(false)
    self.label = self:AddChild(Text(BODYTEXTFONT,35,info))
    self.label:SetPosition(20,0)
    self.label:SetClickable(false)
    self.btn:SetOnClick(fn)
    self.btn:SetOnGainFocus(function()
        self.label:SetString('>> '..info..' <<')
    end)
    self.btn:SetOnLoseFocus(function()
        self.label:SetString(info)
    end)
end)



-- 主面板
---@class widget_webbert_gameconsole : widget_widget
---@field cur_game_id webbert_gameconsole_game_id # 当前浏览的游戏id
---@field bg widget_image # 背景
---@field title_bg widget_image
---@field title_txt widget_text
---@field items widget_webbert_gameconsole_item[]
---@field scrolllist widget_scrollablelist # 左侧游戏菜单
---@field intro_root widget_widget
---@field intro_title_bg widget_image # 右侧介绍标题背景
---@field intro_title_text widget_text # 右侧介绍标题 
---@field intro_content widget_text # 右侧介绍内容
---@field intro_content_width number # SetRegionSize宽度
---@field scroll_area widget_truescrollarea # 滚动区域
---@field startgamebtn widget_imagebutton # 启动游戏按钮
local webbert_gameconsole = Class(Widget,
---@param self widget_webbert_gameconsole
function(self)
    Widget._ctor(self, 'webbert_gameconsole')

    self.cur_game_id = 'tetris'

    self.bg = self:AddChild(Image('images/plantregistry.xml','backdrop.tex','backdrop.tex'))
    self.bg:SetVAnchor(ANCHOR_MIDDLE)
    self.bg:SetHAnchor(ANCHOR_MIDDLE)

    self.title_bg = self.bg:AddChild(Image('images/ui.xml','update_banner.tex','update_banner.tex'))
    self.title_bg:SetPosition(0,300)

    self.title_txt = self.title_bg:AddChild(Text(BODYTEXTFONT,40,STRINGS.MOD_WEBBER_THE_TRAINER.UI.webbert_gameconsole.title))
    self.title_txt:SetPosition(0,10)

    self.items = {}
    for _,v in ipairs(data_webbert_gameconsole.order) do
        local data = data_webbert_gameconsole.games[v]
        local item = webbert_gameconsole_item(data.label,data.logo[1],data.logo[2],function ()
            self:ChangeContentWhenClickGame(v)
        end)
        table.insert(self.items,item)
    end

    self.scrolllist = self.bg:AddChild(webbert_scrollablelist(self.items,165,400,50,2,nil,nil,nil,nil,nil,nil,nil,nil,'webbert_style'))
    self.scrolllist:SetPosition(-160,0)


    -- 右侧 
    self.intro_root = self.bg:AddChild(Widget('intro_root'))
    self.intro_root:SetPosition(170,0)

    self.intro_title_bg = self.intro_root:AddChild(Image('images/scrapbook.xml','scrap2_thin.tex','scrap2_thin.tex'))
    self.intro_title_bg:SetPosition(0,200)
    self.intro_title_bg:SetScale(.24,.24)
    self.intro_title_text = self.intro_root:AddChild(Text(BODYTEXTFONT,40,''))
    self.intro_title_text:SetPosition(0,200)

    local area_root =   self.intro_root:AddChild(Widget("area_root")) -- TrueScrollArea root
    local sub_root = self.intro_root:AddChild(Widget("text_root")) -- 子 root
    local width = 280 -- 实际 宽
    local height = 2000 -- 实际 高
    local max_visible_height = 330 -- 可视区域高
    local padding = 0 -- keep 0
    local height_per_scroll = 23 * 1 -- 每次滚动高度
    self.intro_content_width = width-20 -- 文本widget RegionSize 宽
    local items_txt_height = 2000 -- 文本widget RegionSize 高
    local items_txt_fontsize = 30 -- 文本字体大小
    self.intro_content = sub_root:AddChild(Text(BODYTEXTFONT,items_txt_fontsize,''))
    self.intro_content:SetHAlign(ANCHOR_LEFT)
    self.intro_content:SetVAlign(ANCHOR_TOP)
    self.intro_content:SetRegionSize(self.intro_content_width,items_txt_height) -- 设置文本区域大小

    local top = math.min(height, max_visible_height)/2 - padding

    local scissor_data = {x = 0, y = -max_visible_height/2, width = width, height = max_visible_height}
    local context = {widget = sub_root, offset = {x = width/2, y = top-items_txt_height/2}, size = {w = width, height = height + padding} }
    local scrollbar = { scroll_per_click = height_per_scroll }

    self.scroll_area = area_root:AddChild(TrueScrollArea(context, scissor_data, scrollbar))
    self.scroll_area:SetPosition(-150,0)
    self.scroll_area:Hide()

    self.startgamebtn = self.intro_root:AddChild(TEMPLATES.StandardButton(function()
        self:Hide()
    end , STRINGS.MOD_WEBBER_THE_TRAINER.UI.webbert_gameconsole.startgamebtn, {100,50}))
    self.startgamebtn:SetPosition(0,-200)

    if ThePlayer then
        ThePlayer:DoTaskInTime(0,function()
            self:ChangeContentWhenClickGame(self.cur_game_id)
        end)
    end
end)

function webbert_gameconsole:ChangeContentWhenClickGame(gameid)
    local data = data_webbert_gameconsole.games[gameid]
    -- 显示滚动区域
    if not self.scroll_area.shown then
        self.scroll_area:Show()
    end
    -- 更新title
    self.intro_title_text:SetString(data.intro_title)
    -- 更新滚动框
    local fixed_content = SUGAR_webber_the_trainer:limitMultiLineStringSingleLineMaxLen(data.intro_detail,16)
    local num_lines = self.intro_content:SetMultilineTruncatedString(fixed_content,100, self.intro_content_width,nil,nil,false,16,'\n')
    if num_lines then
        local real_height = num_lines * 25
        self.scroll_area.scroll_pos_end = math.max(0,real_height - 310)
    end
    self.scroll_area:ResetScroll()
    -- 更新按钮状态
    if data.playable then
        self.startgamebtn:Enable()
    else
        self.startgamebtn:Disable()
    end
end


return webbert_gameconsole